package gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JScrollBar;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

import java.awt.Color;
import javax.swing.AbstractListModel;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controllers.LibrarianUIP;
import controllers.OBLSettings;
import controllers.SubscriberUIP;

import messagesPacks.BookSubjectMessage;
import messagesPacks.LibrarianEditBookMessage;
import messagesPacks.LibrarianSearchUserMessage;
import messagesPacks.TOCMessage;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import java.awt.Component;
import java.io.File;
import java.io.Serializable;

import javax.swing.border.TitledBorder;
import files.Browse;
import files.MyFile;
import files.FilenameUtils;

/**
 * This class representing a Librarian Gui Add/Edit book
 * Date: 29/12/2012
 * @author Tali Pisarev
 *
 */


public class LibrarianAddEditBookUI extends JPanel 
{
   private static final long serialVersionUID = 1L;
   private LibrarianUIP luip=null;
	public JTextField textISBN;
	JTextField textBookName;
	JTextField textBookAuthor;
	JTextField textEditionNumber;
	private JLabel lblByName;
	private JLabel lblByAuthor;
	private JLabel lblBySubject;
	private JLabel lblFreeText;
	private JSeparator separator;
	JLabel lblTitleLabel;
    private JButton btnAddBook;
    private JButton btnBack;
    private JButton btnSelectSubject;
    private JLabel lblPublishYear;
    JTextField textPublishYear;
    private JLabel lblDescription;
    private JLabel lblShelfLocation;
    private JLabel lblToc;
    JTextField textDescription;
    JTextField textShelfLocation;
    JTextField textWanted;
    private JLabel lblSubject;
    JList listAllSubjects;
    JList listSelectedSubject;
    JLabel lblAllSubjects;
    JLabel lblSelectedSubjects;
    //private final String[] listSubjects={"History", "Programming","Travel","Medicine","Mathematics"};  //Temporary need to take data from DB
    int selectedSubject;  //temporary
    LibrarianUI librarianPanel=null;
    LibrarianSearchBookUI librarianSearchBookPanel=null;
    //LibrarianSearchBookUI librarianSearchBookPanel=null;
    JButton btnExitButton;
    JButton btnLogoutButton;
    JButton btnBookOperation;
    private JButton btnAddSubject;
    JCheckBox chckbxToc;
    private JTextField textSubjectName;
    JRadioButton rdbtnAddNewSubject;
    private JLabel backgroundlabel;
    
    
    /**hashtable AllBookSubjects for all subjects*/
    Hashtable<Integer,String > AllBookSubjects = new Hashtable <Integer,String>();
    String BookSubject ;
    String BookSubjectID ;
    
    /**hashtable selectBookSubjects for all selected subjects*/
    Hashtable<Integer,String > selectBookSubjects = new Hashtable <Integer,String>();
    String selectedBookSubject ;
    Integer selectedBookSubjectID ;
    
    /**hashtable newBookSubjects for all new subjects for the book*/
    Hashtable<Integer,String > newBookSubjects = new Hashtable <Integer,String>();
    String newBookSubject ;
    Integer newBookSubjectID ;
    
    
    int showAddnewSubjectFlag = 0 ;
 
    /**listModel listAllSubjectsOBJ for subjects for the book*/
	 DefaultListModel<String> listAllSubjectsOBJ;
	 JList listAllSubjectsLog;
	 JScrollPane scrollPane;
	
	 /**listModel listSelectSubjectsOBJ for selsected subjects for the book*/
	 DefaultListModel<String> listSelectSubjectsOBJ;
	 JList listSelectSubjectsLog;
	 JScrollPane scrollPane_2;
    
	//private JPanel contentPane;
	private JButton btnRemove;
	private JTextField textSubjectID;
	private JLabel lblInsertSubjectID;
	private JLabel lblInsertSubjectname;
	
	
	/**FILE TOCFile for the TOC file*/
	 File TOCFile = null;
	 byte [] bytesOFfile = null;
	 JTextField TOCtextField;

//===============================================================================================
//	 Constructor
//	 
/**
 * Constructor the LibrarianAddEditBookUI with default values
 * @param librarianUI
 */

	public LibrarianAddEditBookUI(LibrarianUI librarianUI) 
	{
		super();
		this.librarianPanel=librarianUI;
		this.luip=new LibrarianUIP();
		initialize();	
	}

	
	private void initialize()
	{
	   this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
			
		
		textISBN = new JTextField();
		textISBN.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textISBN.setBounds(123, 71, 173, 34);
		add(textISBN);
		textISBN.setColumns(10);
		
		textBookName = new JTextField();
		textBookName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textBookName.setBounds(123, 123, 173, 34);
		add(textBookName);
		textBookName.setColumns(10);
		
		textBookAuthor = new JTextField();
		textBookAuthor.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textBookAuthor.setBounds(123, 175, 173, 34);
		add(textBookAuthor);
		textBookAuthor.setColumns(10);
		
		textEditionNumber = new JTextField();
		textEditionNumber.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textEditionNumber.setBounds(123, 227, 173, 34);
		add(textEditionNumber);
		textEditionNumber.setColumns(10);
		
		lblByName = new JLabel("ISBN :");
		lblByName.setBounds(10, 71, 51, 31);
		lblByName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblByName);
		
		lblByAuthor = new JLabel("Book Name :");
		lblByAuthor.setBounds(10, 126, 93, 24);
		lblByAuthor.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblByAuthor);
		
		lblBySubject = new JLabel("Author Name :");
		lblBySubject.setBounds(10, 178, 103, 24);
		lblBySubject.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblBySubject);
		
		lblFreeText = new JLabel("Edition Number :");
		lblFreeText.setBounds(10, 230, 109, 24);
		lblFreeText.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblFreeText);
		
		separator = new JSeparator();
		separator.setBounds(-17, 57, 898, 24);
		add(separator);
		
		lblTitleLabel = new JLabel("OBL   Add New Book");
		lblTitleLabel.setBounds(267, -1, 247, 60);
		lblTitleLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		add(lblTitleLabel);
		
		btnLogoutButton = new JButton("Logout");
		btnLogoutButton.setBounds(603, 11, 83, 41);
		btnLogoutButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnLogoutButton);
		
		btnExitButton = new JButton("Exit");
		btnExitButton.setBounds(696, 11, 83, 41);
		btnExitButton.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnExitButton);
		
		if (librarianPanel.op_Book.equals("AddBook")) 
			btnBookOperation = new JButton("Add Book");
		else
			btnBookOperation = new JButton("Edit Book");
		
		btnBookOperation.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnBookOperation.setBounds(321, 505, 142, 41);
		add(btnBookOperation);
		
	
		
		btnBack = new JButton("Back");
		btnBack.setBounds(10, 11, 83, 41);
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		add(btnBack);
		
		btnSelectSubject = new JButton(">>");
		btnSelectSubject.setBounds(522, 206, 57, 41);
		btnSelectSubject.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(btnSelectSubject);
		
		lblPublishYear = new JLabel("Publish Year :");
		lblPublishYear.setBounds(10, 282, 93, 24);
		lblPublishYear.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblPublishYear);
		
		textPublishYear = new JTextField();
		textPublishYear.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textPublishYear.setBounds(123, 279, 173, 34);
		textPublishYear.setColumns(10);
		add(textPublishYear);
		
		lblDescription = new JLabel("Description :");
		lblDescription.setBounds(10, 334, 93, 24);
		lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblDescription);
		
		lblShelfLocation = new JLabel("Shelf Location :");
		lblShelfLocation.setBounds(10, 386, 103, 24);
		lblShelfLocation.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblShelfLocation);
		
		lblToc = new JLabel("Wanted :");
		lblToc.setBounds(10, 438, 83, 24);
		lblToc.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblToc);
		
		textDescription = new JTextField();
		textDescription.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textDescription.setBounds(123, 331, 173, 34);
		textDescription.setColumns(10);
		add(textDescription);
		
		textShelfLocation = new JTextField();
		textShelfLocation.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textShelfLocation.setBounds(123, 383, 173, 34);
		textShelfLocation.setColumns(10);
		add(textShelfLocation);
		
		textWanted = new JTextField();
		textWanted.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textWanted.setBounds(123, 435, 173, 34);
		textWanted.setColumns(10);
		add(textWanted);
		
		lblSubject = new JLabel("Subject :");
		lblSubject.setBounds(323, 71, 103, 31);
		lblSubject.setFont(new Font("Tahoma", Font.BOLD, 16));
		add(lblSubject);

		
		lblAllSubjects = new JLabel("All Subjects");
		lblAllSubjects.setBounds(388, 113, 126, 34);
		lblAllSubjects.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(lblAllSubjects);
		
		lblSelectedSubjects = new JLabel("Selected Subjects");
		lblSelectedSubjects.setBounds(653, 111, 126, 34);
		lblSelectedSubjects.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(lblSelectedSubjects);

	    
	    btnAddSubject = new JButton("Add Subject");
	    btnAddSubject.setBounds(343, 461, 85, 34);
	    btnAddSubject.setFont(new Font("Tahoma", Font.PLAIN, 9));
	    add(btnAddSubject);
	    btnAddSubject.setVisible(false);
	    
	    chckbxToc = new JCheckBox(" Add TOC");
	    chckbxToc.setOpaque(false);
	    chckbxToc.setBounds(10, 504, 97, 23);
	    chckbxToc.setFont(new Font("Tahoma", Font.PLAIN, 12));
	    add(chckbxToc);
	    
	    textSubjectName = new JTextField();
	    textSubjectName.setFont(new Font("Tahoma", Font.PLAIN, 13));
	    textSubjectName.setBounds(337, 428, 99, 27);
	    add(textSubjectName);
	    textSubjectName.setColumns(10);
	    textSubjectName.setVisible(false);
	    
	    rdbtnAddNewSubject = new JRadioButton("Add New Subject");
	    rdbtnAddNewSubject.setOpaque(false);
	    rdbtnAddNewSubject.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	    rdbtnAddNewSubject.setBounds(333, 331, 109, 23);
	    add(rdbtnAddNewSubject);
	   
	    
	    SubscriberUIP subUIP  =  new SubscriberUIP();
	    BookSubjectMessage BookSubject_msg ;
	    BookSubject_msg = subUIP.getSubjectsOfBooks();
	    
	    
	    listAllSubjectsOBJ = new DefaultListModel<String>();
		listAllSubjectsLog = new JList(listAllSubjectsOBJ);
		add(listAllSubjectsLog);
		listAllSubjectsLog.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		listAllSubjectsLog.setBounds(468, 142, 118, 130);
	
		
		scrollPane = new JScrollPane(listAllSubjectsLog);
		scrollPane.setBounds(310, 142, 212, 173);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		add(scrollPane);
		
		listSelectSubjectsOBJ = new DefaultListModel<String>();
		listSelectSubjectsLog = new JList(listSelectSubjectsOBJ);
		add(listSelectSubjectsLog);
		listSelectSubjectsLog.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
		listSelectSubjectsLog.setBounds(468, 142, 118, 130);
		
		
		scrollPane_2 = new JScrollPane(listSelectSubjectsLog);
		scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane_2.setBounds(579, 142, 215, 173);
		add(scrollPane_2);
		

	    
	    //load the books subjects in to the hashtable 
	    this.AllBookSubjects.putAll(BookSubject_msg.getBookSubjects());
	    
	    
	    Enumeration e1 = BookSubject_msg.getBookSubjects().elements();
	    while ( e1.hasMoreElements() )
	    	listAllSubjectsOBJ.addElement((String)e1.nextElement());

	    
		btnRemove = new JButton("Remove ");
		btnRemove.setBounds(679, 325, 85, 34);
		add(btnRemove);
		
		textSubjectID = new JTextField();
		textSubjectID.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textSubjectID.setBounds(337, 373, 99, 27);
		add(textSubjectID);
		textSubjectID.setColumns(10);
		textSubjectID.setVisible(false);
		
		lblInsertSubjectID = new JLabel("Insert SubjectID");
		lblInsertSubjectID.setBounds(343, 357, 93, 14);
		add(lblInsertSubjectID);
		lblInsertSubjectID.setVisible(false);
		
		lblInsertSubjectname = new JLabel("Insert SubjectName");
		lblInsertSubjectname.setBounds(337, 409, 99, 14);
		add(lblInsertSubjectname);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setOrientation(SwingConstants.VERTICAL);
		separator_1.setBounds(306, 57, 2, 543);
		add(separator_1);
		
		TOCtextField = new JTextField();
		TOCtextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		TOCtextField.setBounds(123, 502, 172, 28);
		TOCtextField.setColumns(10);
		TOCtextField.setVisible(false);
		add(TOCtextField);
		

		backgroundlabel = new JLabel("");
		backgroundlabel.setIcon(OBLSettings.IMG_LIBRARIAN_USER_BOOK_COPY);
		backgroundlabel.setBounds(0, 10, 800, 600);
		add(backgroundlabel);
		
		
		lblInsertSubjectname.setVisible(false);
		

	    
	    
// ===========================================================================================
//	Action Listeners
//============================================================================================		
//		
//          //add subject button
			btnSelectSubject.addActionListener(new ActionListener() 
			{
				@SuppressWarnings("deprecation")
				public void actionPerformed(ActionEvent arg0) 
				{
					listSelectSubjectsOBJ.addElement((String)listAllSubjectsLog.getSelectedValue());
					
				} 	
			
			});
//==================================================================================================
			//back button returns to Librarian Panel
			btnBack.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					textISBN.setText(null);
					textBookName.setText(null);
					textBookAuthor.setText(null);
					textEditionNumber.setText(null);
					textPublishYear.setText(null);
					textDescription.setText(null);
					textShelfLocation.setText(null);
					textWanted.setText(null);
					listSelectSubjectsOBJ.removeAllElements();
					if (librarianPanel.op_Book.equals("AddBook"))
						WelcomeUI.wui.switchPanel("OBL  - Librarian",librarianPanel, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
					else if (librarianPanel.op_Book.equals("EditBook"))
						WelcomeUI.wui.switchPanel("OBL  - Librarian",librarianPanel.librarianSearchBookPanel, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
				}
			});
//====================================================================================================
			//Add/Edit book
			btnBookOperation.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{	
					//insert selected subjects into hash table of selected subjects
					String selectSubjectItem; 
					for (int i = 0; i < listSelectSubjectsLog.getModel().getSize(); i++) 
					 {
						    selectSubjectItem = (String)listSelectSubjectsLog.getModel().getElementAt(i);
				            Set set = AllBookSubjects.entrySet();
							Iterator it = set.iterator();
							    while (it.hasNext()) 
							    {
							        Map.Entry entry = (Map.Entry) it.next();
							        if(entry.getValue().equals(selectSubjectItem))
							        { 
							    	  //System.out.println(entry.getKey() + " : " + entry.getValue());
							    	  selectedBookSubject = entry.getValue().toString();
							    	  selectedBookSubjectID =  (Integer) entry.getKey();
							    	  selectBookSubjects.put(selectedBookSubjectID, selectedBookSubject);

							        }//if
							    }//while
					 }// for
					
					if (librarianPanel.op_Book.equals("AddBook"))
					{
						if (!getISBN().isEmpty() &&  !getBookName().isEmpty() && !getBookAuthor().isEmpty() && !getEditionNum().isEmpty() 
								&& !getPublishYear().isEmpty() && !getDescription().isEmpty()
								&& !getShelfLocation().isEmpty() && !getWanted().isEmpty() && !listSelectSubjectsOBJ.isEmpty()) 
						{
						
							int createBookRes=luip.librarianAddBook(getISBN(),getBookName(),getBookAuthor(),getEditionNum(),getPublishYear()
																  ,getDescription(),getShelfLocation(),getWanted(),selectBookSubjects , bytesOFfile);	
							switch (createBookRes)
							{
								
								case 1: // Book created successfully
								{
									textISBN.setText(null);
									textBookName.setText(null);
									textBookAuthor.setText(null);
									textEditionNumber.setText(null);
									textPublishYear.setText(null);
									textDescription.setText(null);
									textShelfLocation.setText(null);
									textWanted.setText(null);
									listSelectSubjectsOBJ.removeAllElements();
						
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Book created successfully!","Create Book",JOptionPane.INFORMATION_MESSAGE);
									break;
								} 
								case -1: // Book already exists
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Book already exists!","Warning",JOptionPane.WARNING_MESSAGE);
									break;
								}
								default: // Error
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
									break;
								}
							} // End switch			
						}
						else //if
							JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);
					
					} // AddBook
					else 
					{  //Edit
						if (!getISBN().isEmpty() &&  !getBookName().isEmpty() && !getBookAuthor().isEmpty() && !getEditionNum().isEmpty() 
								&& !getPublishYear().isEmpty() && !getDescription().isEmpty()
								&& !getShelfLocation().isEmpty() && !getWanted().isEmpty() && !listSelectSubjectsOBJ.isEmpty())
						{
							int editBookRes=luip.librarianEditBook(getISBN(),getBookName(),getBookAuthor(),getEditionNum(),getPublishYear()
															     ,getDescription(),getShelfLocation(),getWanted(),selectBookSubjects , bytesOFfile);
							switch (editBookRes)
							{
								case 1: // book update successfully
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, " Edit succeded ","Done",JOptionPane.INFORMATION_MESSAGE);
			                        break;								
								}
								case -1: // Error
								{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
								break;
								}
								default: // Error
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
									break;
								}
							}//switch
						}//if
					else 
						JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);

					}

				  }		
			});
			
//================================================================================================================
			//radio button
			rdbtnAddNewSubject.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
						if(showAddnewSubjectFlag == 0)
						{
							textSubjectName.setVisible(true);
							textSubjectID.setVisible(true);
							lblInsertSubjectID.setVisible(true);
							lblInsertSubjectname.setVisible(true);							
							btnAddSubject.setVisible(true);
							showAddnewSubjectFlag = 1;
						}
						else if(showAddnewSubjectFlag ==1)
						{
							textSubjectName.setVisible(false);
							textSubjectID.setVisible(false);
							lblInsertSubjectID.setVisible(false);
							lblInsertSubjectname.setVisible(false);							
							btnAddSubject.setVisible(false);
							showAddnewSubjectFlag = 0;
						}
		
				}
				
			});
			
//=======================================================================================================	
			btnRemove.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					String selectSubjectItem;
					selectSubjectItem=(String) listSelectSubjectsLog.getSelectedValue();
					listSelectSubjectsOBJ.removeElement(listSelectSubjectsLog.getSelectedValue());
					//insert selected subjects into hash table of selected subjects
					 System.out.println(selectSubjectItem);
				           Set set1 = selectBookSubjects.entrySet();
							Iterator it1 = set1.iterator();
							   while (it1.hasNext()) 
							    {
								   		Map.Entry entry = (Map.Entry) it1.next();
							        	if(entry.getValue().equals(selectSubjectItem))
							        	{ 
							     		  it1.remove();
							     		  
							     		  System.out.println(entry.getKey() + " : " + entry.getValue());
							    	    }//if

							    }//while

					
				}
			});
			
//======================================================================================================
			btnAddSubject.addActionListener(new ActionListener() 
			{
		    	public void actionPerformed(ActionEvent arg0) 
		    	{
		    		String subjectIDStr=getNewSubjectID();
		    		int subjectID=Integer.parseInt(subjectIDStr);
		    		String subjectName=getNewSubjectName();
		    		newBookSubjects.put(subjectID, subjectName);
		    		int createSubRes=luip.librarianAddBook(null, null, null, null,null,null,null,null,newBookSubjects , null);
		    		if (!getNewSubjectID().isEmpty() &&  !getNewSubjectName().isEmpty())
					{
						switch (createSubRes)
						{
							
							case 1: // Subject created successfully
							{
								textSubjectID.setText(null);
								textSubjectName.setText(null);

								JOptionPane.showMessageDialog( WelcomeUI.wui, " New subject added successfully!","Create Book",JOptionPane.INFORMATION_MESSAGE);
								break;
							} 
							case -1: // subject already exists
							{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Subject already exists!","Warning",JOptionPane.WARNING_MESSAGE);
								break;
							}
							default: // Error
							{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
								break;
							}
						} // End switch		
						
						SubscriberUIP subUIP  =  new SubscriberUIP();
					    BookSubjectMessage BookSubject_msg ;
					    BookSubject_msg = subUIP.getSubjectsOfBooks();
					    AllBookSubjects.putAll(BookSubject_msg.getBookSubjects());
					    //remove old subjects
					    listAllSubjectsOBJ.removeAllElements();
					    //update list
					    Enumeration e1 = BookSubject_msg.getBookSubjects().elements();
					    while ( e1.hasMoreElements() )
					    	listAllSubjectsOBJ.addElement((String)e1.nextElement());

					}//if
		    	
				else //if
					JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);
		    		
		    	}//actionPerformed
		    });
		
			
//======================================================================================================		
			chckbxToc.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent actionEvent) 
			    {	 
					String isbn = textISBN.getText();
					
					TOCtextField.setVisible(false);
			    	TOCtextField.setText("");
			        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
			        boolean selected = abstractButton.getModel().isSelected();
			        if(selected)
			        {  
			        	Browse b = new Browse(WelcomeUI.wui);
						if (b.getFile() != null)
						{	
							System.out.println("b = " + b.getFile().getName());
							MyFile file = b.getFile();
							
							String fileExtOnly = FilenameUtils.getExtension(file.getName());
							String newFileName = isbn + "." + fileExtOnly;
							file.setName(newFileName);
							
							int upload_res = luip.uploadPDF(file,isbn);
							if (upload_res == 1)
							{
								TOCtextField.setVisible(true);
								TOCtextField.setText(file.getName());
								JOptionPane.showMessageDialog( WelcomeUI.wui, "TOC file Uploaded successfully!","TOC Upload",JOptionPane.INFORMATION_MESSAGE);
							}
							else
							{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Uploading failed!","TOC Upload",JOptionPane.WARNING_MESSAGE);
								chckbxToc.setSelected(false);
							}
						}
						else
						{
							System.out.println("file chooser canceled");
							TOCtextField.setVisible(false);
							chckbxToc.setSelected(false);
						}
			        }
			        else
			        {
			        	System.out.println("Delete TOC?");
			        	int dialogButton = JOptionPane.YES_NO_OPTION;
		                JOptionPane.showConfirmDialog (null, "Delete the TOC file for this book?","Delete TOC",dialogButton);
		                if (dialogButton == JOptionPane.YES_OPTION)
		                {
		                	int delete_res = luip.deleteTOC(isbn);
		                	if (delete_res == 1)
		                	{
		                		System.out.println("Deleted the TOC file!!!");
		                		TOCtextField.setVisible(false);
		                		TOCtextField.setText("");
		                		chckbxToc.setSelected(false);
		                		JOptionPane.showMessageDialog( WelcomeUI.wui, "TOC file deleteed successfully!","Delete TOC",JOptionPane.INFORMATION_MESSAGE);
		                	}
							else
							{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Delete of TOC file failed!","Delete TOC",JOptionPane.WARNING_MESSAGE);
							}		                		
		                }
			        }
			    }
	        });
	}//init
	
//====================================================================================================
//SETTERS & GETTERS 
//====================================================================================================
	public String getISBN()
	{
		return this.textISBN.getText();
	}

	
	public String getBookName()
	{
		return this.textBookName.getText();
	}
	
	public String getBookAuthor()
	{
		return this.textBookAuthor.getText();
	}
	
	public String getEditionNum()
	{
		return this.textEditionNumber.getText();
	}
	
	public String getPublishYear()
	{
		return this.textPublishYear.getText();
	}
	
	public String getDescription()
	{
		return this.textDescription.getText();
	}
	
	public String getShelfLocation()
	{
		return this.textShelfLocation.getText();
	}
	
	public String getWanted()
	{
		if(this.textWanted.getText().equals("yes"))
		 return "1";
		else
		 return "0";
	}

	public String getNewSubjectName()	
	{
		return this.textSubjectName.getText();
	}
	public String getNewSubjectID()	
	{
		return this.textSubjectID.getText();
	}
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}
	
	

